package com.application.DataProvider;

import java.sql.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.application.DTO.TeacherDTO;

public class TeacherDAO {
Context _mcontext = null;
	
	public TeacherDAO(Context context){
		this._mcontext = context;
	}
	
	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Teacher where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	public int getIDTeacher(String userName)
	{
		int idTeacher = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select id from Teacher where userName = " + userName;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				if(cur.getString(0).equals(""))//ko co j
					
				idTeacher = cur.getInt(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return idTeacher;
	}
	public TeacherDTO getTeacher(int ID)
	{
		TeacherDTO tDto = new TeacherDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from Teacher where ID = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				tDto.set_ID(Integer.valueOf(cur.getString(0)));
				tDto.set_Fullname(cur.getString(1));
				tDto.set_Birthday(Date.valueOf(cur.getString(2)));
				tDto.set_Address(cur.getString(3));
				tDto.set_Phone(cur.getString(4));
				tDto.set_Username(cur.getString(5));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return tDto;
	}

	public String getFullNameTeacher(int ID)
	{
		String fullName = "";
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select FullName from Teacher where ID = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				fullName = cur.getString(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return fullName;
	}
	
	//Insert du lieu vao bang Teacher
	public int insertTeacher(TeacherDTO tDto) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", tDto.get_ID());
			values.put("FullName", tDto.get_Fullname());
			if(tDto.get_Birthday() != null)
				values.put("Birthday", tDto.get_Birthday().toString());
			else
				values.put("Birthday", "");
			if(tDto.get_Address() != null)
				values.put("Address", tDto.get_Address());
			else
				values.put("Address", "");
			if(tDto.get_Phone() != null)
				values.put("Phone", tDto.get_Phone());
			else
				values.put("Phone", "");
			if(tDto.get_Username() != null)
				values.put("UserName", tDto.get_Username());
			else
				values.put("UserName", "");
			
			result = provider.execInsertCommand(db, "Teacher", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Insert du lieu vao bang Teacher
	public int insertTeacher(int id, String fullName) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", id);
			values.put("FullName", fullName);
			values.put("Birthday", "");
			values.put("Address", "");
			values.put("Phone", "");
			values.put("UserName", "");
			
			result = provider.execInsertCommand(db, "Teacher", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	//Cap nhat bang Teacher
	public int updateTeacher(TeacherDTO tDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("FullName", tDto.get_Fullname());
			values.put("Birthday", "");
			values.put("Address", tDto.get_Address());
			values.put("Phone", tDto.get_Phone());
			values.put("UserName", tDto.get_Username());
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(tDto.get_ID())};
			result= provider.execUpdateCommand(db, "Chapter", values, whereClause, whereArgs);
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	//Cap nhat bang Teacher
	public int updateTeacher(String fullName, int ID)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("FullName", fullName);
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(ID)};
			result= provider.execUpdateCommand(db, "Teacher", values, whereClause, whereArgs);
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	//Delete doi tuong ChapterDTO
	public int deleteTeacher(int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result=provider.execDeleteCommand(db, "Teacher", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
}
